﻿@{
    ViewBag.Title = "费表安装";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}@using LeSoft.Application.Code;
<script>
    var _organizeid = "@OperatorProvider.Provider.Current().CompanyId", _pstructId = "", _allcode = "", WIDTH = "";
    $(function () {
        InitialPage();
        GetTree();
        GetGrid();
        WIDTH = $('.gridPanel').width();
    });
    //初始化页面
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 165.5);
            }, 200);
            e.stopPropagation();
        });
        //组织机构
        $("#PStructTree").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            description: "==请选择组织机构==",
            height: "250px",
            allowSearch: true,
            click: function (item) {
                _organizeid = item.id;
                _allcode = item.title;
                GetTree();
                $('#btn_Search').trigger("click");
            }
        });
        $("#PStructTree").ComboBoxTreeSetValue(_organizeid);
    }
    function GetTree() {
        var item = {
            height: $(window).height() - 55.5,
            url: "../../ResourceManage/RS_PStructs/GetTreeJson?IsSubstep=true&type=5&organizeid=" + _organizeid,
            onnodeclick: function (item) {
                _pstructId = item.id;
                $('#btn_Search').trigger("click");
            }
        };
        //初始化
        $("#itemTree").treeview(item);
    }
    function BaseClick(ss) {

        if (ss == 0) {
            GetGrid1();
            $('#gridTable').setGridWidth(($('.gridPanel').width() < 1 ? WIDTH : $('.gridPanel').width()));
        }
        else if (ss == 1) {
            GetGrid2();
            $('#gridTable2').setGridWidth(($('.gridPanel').width() < 1 ? WIDTH : $('.gridPanel').width()));
        }
        else if (ss == 2) {
            GetGrid3();
            $('#gridTable3').setGridWidth(($('.gridPanel').width() < 1 ? WIDTH : $('.gridPanel').width()));
        }
    }

    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        var queryJson = {
            Nature: "1",
            condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
            keyword: $("#txt_Keyword").val(),
            PStructId: _allcode
        };
        $gridTable.jqGrid({
            url: "../../PropertyManage/CS_EnergyMeterSetting/GetInstanceList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 165.5,
            autowidth: true,
            colModel: [
                { label: "主键", name: "Id", hidden: true },               
                { label: "表编号", name: "Code", index: "Code", width: 100, align: "center", sortable: false },
                { label: "表名称", name: "Name", index: "Name", width: 100, align: "center", sortable: false },
                {
                    label: "父表", name: "ParentId", index: "ParentId", width: 300, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return cellvalue == "0" ? "" : cellvalue;
                    }
        
                },
                { label: "所属房产", name: "PStructId", index: "PStructId", width: 300, align: "center", sortable: false },
                { label: "表位置", name: "PSMemo", index: "PSMemo", width: 100, align: "center", sortable: false },
                {
                    label: "表种类", name: "Type", index: "Type", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return cellvalue == null ? "" : top.clientdataItem["EnergyMeterType"][cellvalue];
                    }
                },
                {
                    label: "表性质", name: "Nature", index: "Nature", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        switch (cellvalue) {
                            case "1": return "单元表"; break;
                            case "2": return "公用表"; break;
                            case "3": return "虚拟表"; break;                          
                            default: return ""; break;
                        }
                    }
                },              
                { label: "倍率", name: "Rate", index: "Rate", width: 100, align: "center", sortable: false },
                { label: "单价", name: "UnitPrice", index: "UnitPrice", width: 100, align: "center", sortable: false },
                { label: "量程", name: "Range", index: "Range", width: 100, align: "center", sortable: false },
                { label: "最高用量", name: "MaxVolume", index: "MaxVolume", width: 100, align: "center", sortable: false },
                { label: "最低用量", name: "MinVolume", index: "MinVolume", width: 100, align: "center", sortable: false },
                { label: "表容量", name: "Volume", index: "Volume", width: 100, align: "center", sortable: false },
                { label: "描述", name: "Description", index: "Description", width: 100, align: "center", sortable: false }
            ],
            viewrecords: true,
            treeGrid: true,
            rowNum: "all",
            rownumbers: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = {
                Nature: "1",
                condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
                keyword: $("#txt_Keyword").val(),
                PStructId: _allcode
            };
            $gridTable.jqGrid('setGridParam', {
                url: "../../PropertyManage/CS_EnergyMeterSetting/GetInstanceList",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }
    function GetGrid2() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable2');
        var queryJson = {
            Nature: "2",
            condition: $("#queryCondition2").find('.dropdown-text').attr('data-value'),
            keyword: $("#txt_Keyword2").val(),
            PStructId: _allcode
        };
        $gridTable.jqGrid({
            url: "../../PropertyManage/CS_EnergyMeterSetting/GetInstanceList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 185,
            autowidth: true,
            colModel: [
                { label: "主键", name: "Id", index: "Id", hidden: true },
                { label: "所属区域", name: "PStructId", index: "PStructId", width: 300, align: "center", sortable: false },
                { label: "表编号", name: "Code", index: "Code", width: 100, align: "center", sortable: false },
                { label: "表名称", name: "Name", index: "Name", width: 100, align: "center", sortable: false },
                { label: "表种类", name: "Type", index: "Type", width: 100, align: "center", sortable: false },
                { label: "表性质", name: "Nature", index: "Nature", width: 100, align: "center", sortable: false },
                { label: "表位置", name: "PSMemo", index: "PSMemo", width: 100, align: "center", sortable: false },
                { label: "父表", name: "ParentId", index: "ParentId", width: 300, align: "center", sortable: false },
                { label: "倍率", name: "Rate", index: "Rate", width: 100, align: "center", sortable: false },
                { label: "单价", name: "UnitPrice", index: "UnitPrice", width: 100, align: "center", sortable: false },
                { label: "量程", name: "Range", index: "Range", width: 100, align: "center", sortable: false },
                { label: "最高用量", name: "MaxVolume", index: "MaxVolume", width: 100, align: "center", sortable: false },
                { label: "最低用量", name: "MinVolume", index: "MinVolume", width: 100, align: "center", sortable: false },
                { label: "表容量", name: "Volume", index: "Volume", width: 100, align: "center", sortable: false },
                { label: "描述", name: "Description", index: "Description", width: 100, align: "center", sortable: false }
            ],
            viewrecords: true,
            //treeGrid: true,
            // treeGridModel: "nested",
            // ExpandColumn: "Name",
            // rowNum: "all",
            rowNum: 1000,
            gridview: true,
            rownumbers: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');

            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件
        $("#queryCondition2 .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition2 .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search2").click(function () {
            var queryJson = {
                Nature: "2",
                condition: $("#queryCondition2").find('.dropdown-text').attr('data-value'),
                keyword: $("#txt_Keyword2").val(),
                PStructId: _allcode
            };
            $gridTable.jqGrid('setGridParam', {
                url: "../../PropertyManage/CS_EnergyMeterSetting/GetInstanceList",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }
    function GetGrid3() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable3');
        var queryJson = {
            Nature: "3",
            condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
            keyword: $("#txt_Keyword").val(),
            PStructId: _allcode
        };
        $gridTable.jqGrid({
            url: "../../PropertyManage/CS_EnergyMeterSetting/GetInstanceList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 165.5,
            autowidth: true,
            colModel: [
                { label: "主键", name: "Id", hidden: true },
                { label: "房间名称", name: "PStructId", index: "PStructId", width: 300, align: "center", sortable: false },
                { label: "表编号", name: "Code", index: "Code", width: 100, align: "center", sortable: false },
                { label: "表名称", name: "Name", index: "Name", width: 100, align: "center", sortable: false },
                { label: "表种类", name: "Type", index: "Type", width: 100, align: "center", sortable: false },
                { label: "表性质", name: "Nature", index: "Nature", width: 100, align: "center", sortable: false },
                { label: "表位置", name: "PSMemo", index: "PSMemo", width: 100, align: "center", sortable: false },
                { label: "父表", name: "ParentId", index: "ParentId", width: 300, align: "center", sortable: false },
                { label: "倍率", name: "Rate", index: "Rate", width: 100, align: "center", sortable: false },
                { label: "单价", name: "UnitPrice", index: "UnitPrice", width: 100, align: "center", sortable: false },
                { label: "量程", name: "Range", index: "Range", width: 100, align: "center", sortable: false },
                { label: "最高用量", name: "MaxVolume", index: "MaxVolume", width: 100, align: "center", sortable: false },
                { label: "最低用量", name: "MinVolume", index: "MinVolume", width: 100, align: "center", sortable: false },
                { label: "表容量", name: "Volume", index: "Volume", width: 100, align: "center", sortable: false },
                { label: "描述", name: "Description", index: "Description", width: 100, align: "center", sortable: false }
            ],
            viewrecords: true,
            treeGrid: true,
            treeGridModel: "nested",
            ExpandColumn: "Name",
            rowNum: "all",
            rownumbers: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = {
                Nature: "3",
                condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
                keyword: $("#txt_Keyword").val(),
                PStructId: _allcode
            };
            $gridTable.jqGrid('setGridParam', {
                url: "../../PropertyManage/CS_EnergyMeterSetting/GetInstanceList",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });

        //查询回车
        $('#keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }

    //新增单元表
    function btn_add(Nature) {
        var title = "";
        switch (Nature) {
            case 1: title = "添加单元表"; break;
            case 2: title = "添加公用表"; break;
            case 3: title = "添加虚拟表"; break;
            default: title = "添加单元表"; break;
        }
        dialogOpen({
            id: "Form",
            title: title,
            url: '/PropertyManage/CS_EnergyMeterSetting/UnitForm?Nature=' + Nature + '&PstructId=' + _pstructId,
            width: "1100px",
            height: "800px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    };
    function btn_edit(Nature) {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        var title = "";
        switch (Nature) {
            case 1: title = "添加单元表"; keyValue = $("#gridTable").jqGridRowValue("Id"); break;
            case 2: title = "添加公用表"; keyValue = $("#gridTable2").jqGridRowValue("Id"); break;
            case 3: title = "添加虚拟表"; keyValue = $("#gridTable3").jqGridRowValue("Id"); break;
            default: title = "添加单元表"; keyValue = $("#gridTable").jqGridRowValue("Id"); break;
        }
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "Form",
                title: title,
                url: '/PropertyManage/CS_EnergyMeterSetting/UnitForm?Nature=' + Nature + '&keyValue=' + keyValue,
                width: "1100px",
                height: "800px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            })

        }
    }
    function btn_delete(Nature) {
        var keyValue;
        switch (Nature) {
            case 1: keyValue = $("#gridTable").jqGridRowValue("Id"); break;
            case 2: keyValue = $("#gridTable2").jqGridRowValue("Id"); break;
            case 3: keyValue = $("#gridTable3").jqGridRowValue("Id");break;
            default: keyValue = $("#gridTable").jqGridRowValue("Id"); break;
        }
        if (keyValue) {
            $.RemoveForm({
                url: "/PropertyManage/CS_EnergyMeterSetting/RemoveUnitForm",
                param: { keyValue: keyValue },
                success: function (data) {
                    Renovate(Nature);
                }
            })
        } else {
            dialogMsg('请选择需要删除的数据项！', 0);
        }
    }
    function btn_addmore() {
        dialogOpen({
            id: "Form",
            title: '批量添加单元表',
            url: '//PropertyManage/CS_EnergyMeterSetting/UnitBatchForm',
            width: "1100px",
            height: "800px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    }

    function Renovate(Nature) {  //刷新
        switch (Nature) {
            case 1:
                $("#gridTable").resetSelection();
                $("#gridTable").trigger("reloadGrid"); break;
            case 2:
                $("#gridTable2").resetSelection();
                $("#gridTable2").trigger("reloadGrid"); break;
            case 3:
                $("#gridTable3").resetSelection();
                $("#gridTable3").trigger("reloadGrid"); break;
            default: $("#gridTable").resetSelection();
                $("#gridTable").trigger("reloadGrid"); break;
        }
    }

</script>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <div class="west-Panel">
            <div class="panel-Title" style="padding:0px;"><div id="PStructTree" type="selectTree" class="ui-select"></div></div>
            <div id="itemTree"></div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="center-Panel">
            <ul class="nav nav-tabs" style="background-color:#f6f8f8;">
                <li class="active" onclick="BaseClick(0)">
                    <a href="#panel1" data-toggle="tab">单元表</a>
                </li>
                <li onclick="BaseClick(1)">
                    <a href="#panel2" data-toggle="tab">公用表</a>
                </li>
                <li onclick="BaseClick(2)">
                    <a href="#panel3" data-toggle="tab">虚拟表</a>
                </li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="panel1">
                    <div class="titlePanel">
                        <div class="title-search">
                            <table>
                                <tr>
                                    <td>
                                        <div class="btn-group" id="queryCondition">
                                            <a class="btn btn-default dropdown-text" data-toggle="dropdown">选择条件</a>
                                            <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                            <ul class="dropdown-menu">
                                                <li><a data-value="Code">编号</a></li>
                                                <li><a data-value="Name">名称</a></li>
                                            </ul>
                                        </div>
                                    </td>
                                    <td style="padding-left: 2px;">
                                        <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                                    </td>
                                    <td style="padding-left: 5px;">
                                        <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="toolbar">
                            <div class="btn-group">
                                <a id="lr-replace" class="btn btn-default" onclick="reload();"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                                <a id="lr-add" class="btn btn-default" onclick="btn_add(1)"><i class="fa fa-plus"></i>&nbsp;新增</a>
                                @*<a id="lr-addmore" class="btn btn-default" onclick="btn_addmore()"><i class="fa fa-plus"></i>&nbsp;批量新增</a>*@
                                <a id="lr-edit" class="btn btn-default" onclick="btn_edit(1)"><i class="fa fa-fa-pencil-square-o"></i>&nbsp;编辑</a>
                                <a id="lr-delete" class="btn btn-default" onclick="btn_delete(1)"><i class="fa fa-trash-o"></i>&nbsp;卸载</a>
                            </div>
                            <script>$('.toolbar').authorizeButton()</script>
                        </div>
                    </div>
                    <div class="gridPanel">
                        <table id="gridTable"></table>
                    </div>
                </div>
                <div class="tab-pane" id="panel2">
                    <div class="titlePanel">
                        <div class="title-search">
                            <table>
                                <tr>
                                    <td>
                                        <div class="btn-group" id="queryCondition2">
                                            <a class="btn btn-default dropdown-text" data-toggle="dropdown">选择条件</a>
                                            <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                            <ul class="dropdown-menu">
                                                <li><a data-value="Code">编号</a></li>
                                                <li><a data-value="Name">名称</a></li>
                                            </ul>
                                        </div>
                                    </td>
                                    <td style="padding-left: 2px;">
                                        <input id="txt_Keyword2" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                                    </td>
                                    <td style="padding-left: 5px;">
                                        <a id="btn_Search2" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="toolbar">
                            <div class="btn-group">
                                <a id="lr-replaceDatas" class="btn btn-default" onclick="Renovate(2);"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                                <a id="lr-addPublic" class="btn btn-default" onclick="btn_add(2)"><i class="fa fa-plus"></i>&nbsp;新增</a>
                                <a id="lr-editPublic" class="btn btn-default" onclick="btn_edit(2)"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>
                                <a id="lr-deletePublic" class="btn btn-default" onclick="btn_delete(2)"><i class="fa fa-trash-o"></i>&nbsp;卸载</a>
                            </div>
                            <script>$('.toolbar').authorizeButton()</script>
                        </div>
                    </div>
                    <div class="gridPanel">
                        <table id="gridTable2"></table>
                        <div id="gridPager2"></div>
                    </div>
                </div>
                <div class="tab-pane" id="panel3">
                    <div class="titlePanel">
                        <div class="title-search">
                            <table>
                                <tr>
                                    <td>
                                        <div class="btn-group" id="queryCondition">
                                            <a class="btn btn-default dropdown-text" data-toggle="dropdown">选择条件</a>
                                            <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                            <ul class="dropdown-menu">
                                                <li><a data-value="Code">编号</a></li>
                                                <li><a data-value="Name">名称</a></li>
                                            </ul>
                                        </div>
                                    </td>
                                    <td style="padding-left: 2px;">
                                        <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                                    </td>
                                    <td style="padding-left: 5px;">
                                        <a id="btn_Search3" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="toolbar">
                            <div class="btn-group">
                                <a id="lr-replacVirtuale" class="btn btn-default" onclick="Renovate(3);"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                                <a id="lr-addVirtual" class="btn btn-default" onclick="btn_add(3)"><i class="fa fa-plus"></i>&nbsp;安装</a>
                                @*<a id="lr-editParts" class="btn btn-default" onclick="btn_edit(3)"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>*@
                                <a id="lr-deleteVirtual" class="btn btn-default" onclick="btn_delete(3)"><i class="fa fa-trash-o"></i>&nbsp;卸载</a>
                            </div>
                            <script>$('.toolbar').authorizeButton()</script>
                        </div>
                    </div>
                    <div class="gridPanel">
                        <table id="gridTable3"></table>
                    </div>
                </div>
            </div>


        </div>
    </div>
</div>
